package com.sq.universe.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sq.universe.base.controller.BaseController;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.system.entity.RoleMenuEntity;
import com.sq.universe.system.service.RoleMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roleMenu")
@CrossOrigin
public class RoleMenuController extends BaseController {

    @Autowired
    private RoleMenuService roleMenuService;

    /**
    * 查询数据
    * @param roleMenuEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("system:roleMenu:query")
    public RespEntity query(@RequestBody(required = false) RoleMenuEntity roleMenuEntity){
        try {
            return new RespEntity("查询成功",roleMenuService.query(roleMenuEntity));
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param roleMenuEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:roleMenu:queryByPage")
    public RespEntity queryByPage(@RequestBody(required = false) RoleMenuEntity roleMenuEntity){
        try {
            PageHelper.startPage(roleMenuEntity.getPageNo(),roleMenuEntity.getPageSize());
            List<RoleMenuEntity> roleMenuList = roleMenuService.query(roleMenuEntity);
            PageInfo<RoleMenuEntity> pageInfo = new PageInfo<RoleMenuEntity>(roleMenuList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param roleMenuEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:roleMenu:insert")
    public RespEntity insert(@RequestBody(required = false) RoleMenuEntity roleMenuEntity){
        try {
            roleMenuService.insert(roleMenuEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param roleMenuEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:roleMenu:update")
    public RespEntity update(@RequestBody(required = false) RoleMenuEntity roleMenuEntity){
        try {
            roleMenuService.update(roleMenuEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param roleMenuEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:roleMenu:delete")
    public RespEntity delete(@RequestBody(required = false) RoleMenuEntity roleMenuEntity){
        try {
            if(roleMenuEntity.getRmId() == null){
                return new RespEntity("删除失败");
            }
            roleMenuService.logicalDelete(roleMenuEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            return new RespEntity("删除失败");
        }
    }

    /**
     * 插入
     * @param roleMenuData
     * @return
     */
    @PostMapping("/insertBatch")
    @RequiresPermissions("system:roleMenu:insertBatch")
    public RespEntity insertBatch(@RequestBody(required = false) String roleMenuData){
        try {
            JSONObject jsonObject = JSON.parseObject(roleMenuData);
            String roleCode = jsonObject.getString("roleCode");
            List<String> roleMenuEntities = JSONArray.parseArray(jsonObject.getString("menuArr"),String.class);
            roleMenuService.insertBatch(roleMenuEntities,roleCode,getCurrentUser());
            return new RespEntity("保存成功");
        }catch (Exception exception){
            return new RespEntity("保存失败");
        }
    }
}